NestJS之16 | 您所在的位置:网站首页 › nestjs 中文文档 › NestJS之16 |
1. 安装配置
安装 npm install @nestjs/swagger swagger-ui-express main.ts import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule); const options = new DocumentBuilder() .setTitle('接口文档') .setDescription('描述信息') .setVersion('1.0.0') .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('/api-docs', app, document); await app.listen(3000); } 复制代码guard.controller.ts import { ApiTags } from '@nestjs/swagger'; @Controller('guard') @ApiTags('守卫接口') // 分组 @UseGuards(RoleGuard) 复制代码结合dto进行使用 create-guard.dto.ts import { ApiProperty } from '@nestjs/swagger'; export class CreateGuardDto { @ApiProperty({ example: 'jack', type: String }) name: string; @ApiProperty() age: number; } =====================post中不用写=================== @Post() create(@Body() createGuardDto: CreateGuardDto) { return this.guardService.create(createGuardDto); } 复制代码main.ts const options = new DocumentBuilder() .addBearerAuth() // 此处添加 .setTitle('接口文档') .setDescription('描述信息') .setVersion('1.0.0') .build(); 复制代码guard.controller.ts @Controller('guard') @ApiTags('守卫接口') // 分组 @ApiBearerAuth() @UseGuards(RoleGuard) export class GuardController {} 复制代码 |
CopyRight 2018-2019 实验室设备网 版权所有 |